﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DelonixRegiaHMSys.Core.Interfaces;
using DelonixRegiaHMSys.Core.Models;
using DelonixRegiaHMSys.Infrastructure.Data;
using System.Data.Objects;

namespace DelonixRegiaHMSys.Infrastructure.Data
{
    public class EfSystemAccountQuery : ISystemAccountQuery
    {
        public IEnumerable<SystemAccount> GetAll()
        {
            var context = new hmsDBEntities();
            return context.userAccounts
                .Select(f => new SystemAccount()
                {
                    UserID = f.userID,
                    Password = f.password,
                    AccessLevel = f.accessLevel,
                    StaffID = f.staffID,
                    UserStatus = f.userStatus
                });
        }

        public void AddUser(SystemAccount sa)
        {
            var context = new hmsDBEntities();
            var newUser = context.userAccounts.Create();
            newUser.userID = sa.UserID;
            newUser.password = sa.Password;
            newUser.accessLevel = sa.AccessLevel;
            newUser.staffID = sa.StaffID;
            newUser.userStatus = sa.UserStatus;
            context.userAccounts.Add(newUser);
            context.SaveChanges();
        }

        public void DeleteUser(string userID)
        {
            var context = new hmsDBEntities();
            var userToDelete = context.userAccounts.FirstOrDefault(u => u.userID == userID);
            context.userAccounts.Remove(userToDelete);
            context.SaveChanges();
        }

        public void UpdateUser(SystemAccount sa)
        {
            var context = new hmsDBEntities();
            var userToUpdate = context.userAccounts.FirstOrDefault(u => u.userID == sa.UserID);
            userToUpdate.password = sa.Password;
            userToUpdate.accessLevel = sa.AccessLevel;
            userToUpdate.staffID = sa.StaffID;
            context.SaveChanges();
        }

        public bool DoesTheAccountExist(string userID, string password)
        {
            var context = new hmsDBEntities();
            var accountExist = context.userAccounts.FirstOrDefault(u => u.userID == userID);
            if (accountExist != null && accountExist.password.Equals(password))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void UpdateUserStatus(string userID, string userStatus)
        {
            var context = new hmsDBEntities();
            var userToUpdate = context.userAccounts.FirstOrDefault(u => u.userID == userID);
            userToUpdate.userStatus = userStatus;
            context.SaveChanges();
        }

        public bool IsAdmin(string userID)
        {
            var context = new hmsDBEntities();
            var userIsAdminOrMS = context.userAccounts.FirstOrDefault(u => u.userID == userID);
            if (userIsAdminOrMS.accessLevel == "HA")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool IsAdminOrMS(string userID)
        {
            var context = new hmsDBEntities();
            var userIsAdminOrMS = context.userAccounts.FirstOrDefault(u => u.userID == userID);
            if (userIsAdminOrMS.accessLevel == "HA" || userIsAdminOrMS.accessLevel == "MS")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool IsUserOnline(string userID)
        {
            var context = new hmsDBEntities();
            var userIsOnline = context.userAccounts.FirstOrDefault(u => u.userID == userID);
            if (userIsOnline.userStatus == "ON")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool StaffHasAccount(string staffID)
        {
            var context = new hmsDBEntities();
            var staffHasAccount = context.userAccounts.FirstOrDefault(u => u.staffID == staffID);
            if (staffHasAccount != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
